// ここにメインエンジン

/* グローバル変数 */

// Twitter関連
var consumerKey			= "uVQoMFwJuwSNJ7s4IECwA";
var consumerSecret		= "Q6puiy2c3PLjQVs7qCrR0CahHcESXqzeTjgHl4E";

var accessToken			= undefined;
var accessTokenSecret	= undefined;

var T			= undefined;


function init() {
	twttr.anywhere(onAnywhereLoad);
	function onAnywhereLoad(twitter) {
		//twitter.linkifyUsers();
		T = twitter;
		if (twitter.isConnected()) {
			//sencha_init();
			$("#debug").append('<button id="signout" onclick="twttr.anywhere.signOut();" type="button">Sign out of Twitter</button>'); 
			//screenName = twitter.currentUser.data("screen_name");
			//$("#debug").append($("<p>").text("ScreenName: "+screenName));
			//T("#debug").tweetBox({ height: 100, width: 400, defaultContent: "<YOUR DEFAULT TWEETBOX CONTENT HERE>" });
		}else {
			T("#debug").connectButton({
				authComplete: function(user) {
					// triggered when auth completed successfully
					location.reload();
				},
				signOut: function() {
					// triggered when user logs out
				}
			});
		}
	};
}

function sencha_init() {
	Ext.setup({
		icon: 'img/icon.png',
		glossOnIcon: false
	});

	var timeline = new Ext.Component({
		title: 'Twitter',
		cls: 'twitter_tl',
		scroll: 'vertical',
		tpl: [
			'<tpl for=".">',
				'<div class="tweet">',
						'<div class="avatar"><img src="{profile_image_url}" /></div>',
						'<div class="tweet-content">',
							'<h2>{from_user}</h2>',
							'<p>{text}</p>',
						'</div>',
				'</div>',
			'</tpl>'
		]
	});

	/*var map = new Ext.Map({
		title: 'Map',
		getLocation: true,
		mapOptions: {
			zoom: 12
		}
	});*/

	var panel = new Ext.TabPanel({
		fullscreen: true,
		animation: 'slide',
		items: [timeline]
	});
	
	var refresh = function() {
		;
	}

	/*var refresh = function() {
		var coords = map.geo.coords;

		Ext.util.JSONP.request({
			url: 'http://search.twitter.com/search.json',
			callbackKey: 'callback',
			params: {
				geocode: coords.latitude + ',' + coords.longitude + ',' + '5mi',
				rpp: 30
			},
			callback: function(data) {
				data = data.results;

				// タイムラインのツイートを更新
				timeline.update(data);

				// 地図にマーカーを追加
				for (var i = 0, ln = data.length; i < ln; i++) {
					var tweet = data[i];

					// If the tweet is geo-tagged, use that to display marker
					if (tweet.geo && tweet.geo.coordinates) {
						var position = new google.maps.LatLng(tweet.geo.coordinates[0], tweet.geo.coordinates[1]);
						addMarker(tweet, position);
					}
				}
			}
		});
	};*/

	// 以下は全てGoogle MapsのAPI
	/*var addMarker = function(tweet, position) {
		var marker = new google.maps.Marker({
			map: map.map,
			position: position
		});
	};*/

	//map.geo.on('update', refresh);

	/*var tabBar = panel.getTabBar();
	tabBar.addDocked({
		xtype: 'button',
		ui: 'mask',
		iconCls: 'refresh',
		dock: 'right',
		stretch: false,
		align: 'center',
		handler: refresh
	});*/
}
